package com.yata.utils.html;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HtmlConverter {
	
	public static final String UTF8_BOM = "\uFEFF";
	
	public static List<String> readFile2TextByNIO(String fileName) throws IOException {
		Path path = Paths.get(fileName);
		
		//byte[] bytes = Files.readAllBytes(path);
		//String t = new String(bytes, StandardCharsets.UTF_8);
		//List<String> allLines = new ArrayList<String>(Arrays.asList(t.split(System.lineSeparator())));
		
		List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
		return allLines;
	}
	
	public static List<String> readFile2TextByIO(String fileName) throws IOException {
		// Use this for reading the data.
        byte[] buffer = new byte[1000];

        FileInputStream inputStream = 
            new FileInputStream(fileName);

        // read fills buffer with data and returns
        // the number of bytes read (which of course
        // may be less than the buffer size, but
        // it will never be more).
        int total = 0;
        int nRead = 0;
        String all = "";
        while((nRead = inputStream.read(buffer)) != -1) {
            // Convert to String so we can display it.
            // Of course you wouldn't want to do this with
            // a 'real' binary file.
        	String temp = new String(buffer, 0, nRead, StandardCharsets.UTF_8);
            
            all = all + temp;
            
            total += nRead;
        }   

        // Always close files.
        inputStream.close();        

        System.out.println("Read " + total + " bytes");
        
        List<String> allLines = new ArrayList<String>(Arrays.asList(all.split(System.lineSeparator())));
        
        return allLines;
	}
	
	public static List<String> getString2Text(String htmlString) throws IOException {
		List<String> allLines = new ArrayList<String>(Arrays.asList(htmlString.split("\n")));
		return allLines;
	}
	
	public static String listToString(List<String> list, String separator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			if (i == list.size() - 1) {
				sb.append(list.get(i));
			} else {
				sb.append(list.get(i));
				sb.append(separator);
			}
		}
		return sb.toString();
	}
	
	public static String getPureHtmlFromJSFile(String fileName) throws IOException {
		// 1:忽略单行注释  //
    	// 2:去掉第一个加号+ 第一个单引号' 最后一个单引号'
		List<String> ls = HtmlConverter.readFile2TextByNIO(fileName);
		
		return _js2html(ls);
	}
	
	public static String getPureHtmlFromJSString(String jsString) throws IOException {
		// 1:忽略单行注释  //
    	// 2:去掉第一个加号+ 第一个单引号' 最后一个单引号'
		List<String> ls = HtmlConverter.getString2Text(jsString);
		
		return _js2html(ls);
	}
	
	public static String getJSFromHtmlFile(String fileName) throws IOException {
		
		List<String> ls = HtmlConverter.readFile2TextByNIO(fileName);
		
		return _html2Js(ls);	
	}
	
	public static String getJSFromHtmlString(String htmlString) throws IOException {
		
		List<String> ls = HtmlConverter.getString2Text(htmlString);
		
		return _html2Js(ls);
	}
	
	private static String _html2Js(List<String> htmlLine) throws IOException {
		List<String> newLs = new ArrayList<String>();
		
		for(int i=0; i<htmlLine.size(); i++) {
			String t = htmlLine.get(i);
			t = "'" + t + "'+";
			newLs.add(t);
		}
		
		return HtmlConverter.listToString(newLs, System.lineSeparator());		
	}
	
	private static String _js2html(List<String> jsLine) throws IOException {
		List<String> newLs = new ArrayList<String>();
		
		for(int i=0; i<jsLine.size(); i++) {
			String t = jsLine.get(i).trim();
			
			if ((0 == i) && t.startsWith(UTF8_BOM)) {
				t = t.substring(2);
			}
			
			if (0 == t.indexOf("//")) {
				continue;
			} else {
				t = t.replaceFirst("^\\+\\s*'", "");
				t = t.replaceFirst("'$", "");
				t = t.replaceFirst("'\\s*\\+$", "");
				t = t.replaceFirst("^'", "");
				
				newLs.add(t);
			}
		}
		
		return HtmlConverter.listToString(newLs, "");
	}
}
